package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.PermissionMapper;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;


@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Permission> permissions = permissionDao.findByCondition(queryString);
        long total = permissions.getTotal();
        List<Permission> result = permissions.getResult();
        return new PageResult(total, result);
    }

    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    @Override
    public void permissionAdd(Permission permission) {
        Date date = new Date();
        java.sql.Date sqlDate = new java.sql.Date(date.getTime());
        permission.setCreationTime(sqlDate);
        permissionDao.permissionAdd(permission);
    }

    @Override
    public String delete(Integer id) {
        String s = null;
        try {
            permissionDao.delete(id);
            s = "删除成功";
            return s;
        } catch (Exception e) {
            s = "删除失败,因为权限被角色关联,禁止删除";
            return s;
        }

    }

    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }

    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }

    @Override
    public List<Integer> findPermissionByRoleId(Integer id) {
        return permissionDao.findPermissionByRoleId(id);
    }


}
